﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class UserConTrols_Khuyen_Mai : System.Web.UI.UserControl
{
    DataProvider duLieu = new DataProvider();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //==== Phần Trang ====
            int sohangtren1trang = (int)Application["SoSPTren1TrangKhiPhanTrang"];
            int vitri1 = 1;
            int vitri2 = sohangtren1trang;
            int total = duLieu.LayTongHangHoa("khuyenmai");
            DataTable bang1 = null;
            if (total > sohangtren1trang)
            {
                pnPhanTrang.Visible = true;
            }
            else
            {
                pnPhanTrang.Visible = false;
            }
            string index = Request.QueryString["index"];
            string page = Request.QueryString["page"];

            if (!string.IsNullOrEmpty(index) & page == "next")
            {
                if (int.Parse(index) < sohangtren1trang)
                    index = "1";

                vitri1 = int.Parse(index);
                vitri2 = (int.Parse(index) < sohangtren1trang ? vitri1 + (sohangtren1trang - 1) : int.Parse(index) + sohangtren1trang);

                if (vitri2 > total)
                {
                    vitri2 = total;
                    vitri1 = total - sohangtren1trang;
                }


            }
            else if (!string.IsNullOrEmpty(index) & page == "back")
            {
                if (int.Parse(index) > sohangtren1trang)
                {
                    vitri1 = int.Parse(index) - sohangtren1trang;
                }
                else
                {
                    vitri1 = 0;
                }
                vitri2 = vitri1 + sohangtren1trang;
            }

            bang1 = duLieu.phanTrang(vitri1, vitri2, "select km.id, km.tieude, km.motavantat, km.hinhanh, km.ngaydangtin from khuyenmai km order by ngaydangtin desc");

            trangdautien.NavigateUrl = "../KhuyenMai.aspx?page=back&index=1#pagging";
            trangsau.NavigateUrl = "../KhuyenMai.aspx?page=back&index=" + vitri1 + "#pagging";
            trangtiep.NavigateUrl = "../KhuyenMai.aspx?page=next&index=" + vitri2 + "#pagging";
            trangcuoi.NavigateUrl = "../KhuyenMai.aspx?page=next&index=" + (total - 6) + "#pagging";
            tongconghanghoa.Text = "<font color='blue'>Đang xem</font>&nbsp;<font color='red'>" + vitri1 + " - " + vitri2 + "</font>&nbsp;<font color='blue'>của&nbsp;</font><font color='red'>" + total + "</font>&nbsp;<font color='blue'>khuyến mãi</font>";
          
            //=== Load Dữ Liệu Vào DATALIST ====

            if (bang1.Rows.Count != 0)
            {
                DataList1.DataSource = bang1;
                DataList1.DataBind();
            }
            else
            {
                showthongbao.Text = duLieu.BangThongBao("THÔNG BÁO", "<a href='Default.aspx'>HIỆN TẠI KHÔNG CÓ KHUYẾN MÃI NÀO CẢ</a>");
            }

        }
    }
}